﻿/**  版本信息模板在安装目录下，可自行修改。
* DepartmentTypeRel.cs
*
* 功 能： N/A
* 类 名： DepartmentTypeRel
*
* Ver    变更日期             负责人  变更内容
* ───────────────────────────────────
* V0.01  2013-4-18 15:57:46   N/A    初版
*
* Copyright (c) 2012 Maticsoft Corporation. All rights reserved.
*┌──────────────────────────────────┐
*│　此技术信息为本公司机密信息，未经本公司书面同意禁止向第三方披露．　│
*│　版权所有：动软卓越（北京）科技有限公司　　　　　　　　　　　　　　│
*└──────────────────────────────────┘
*/
using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;

namespace DMZS.DAL
{
    /// <summary>
    /// 数据访问类:DepartmentTypeRel
    /// </summary>
    public partial class DepartmentTypeRel
    {
        public DepartmentTypeRel()
        {
        }

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("ID", "DepartmentTypeRel");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int ID)
        {
            var strSql = new StringBuilder();
            strSql.Append("select count(1) from DepartmentTypeRel");
            strSql.Append(" where ID=@ID");
            var parameters = new SqlParameter[] { new SqlParameter("@ID", SqlDbType.Int, 4)
            };
            parameters[0].Value = ID;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(DMZS.Model.DepartmentTypeRel model)
        {
            var strSql = new StringBuilder();
            strSql.Append("insert into DepartmentTypeRel(");
            strSql.Append("ParentID,ChildID)");
            strSql.Append(" values (");
            strSql.Append("@ParentID,@ChildID)");
            strSql.Append(";select @@IDENTITY");
            var parameters = new SqlParameter[] { new SqlParameter("@ParentID", SqlDbType.Int, 4),
                    new SqlParameter("@ChildID", SqlDbType.Int, 4) };
            parameters[0].Value = model.ParentID;
            parameters[1].Value = model.ChildID;

            var obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(DMZS.Model.DepartmentTypeRel model)
        {
            var strSql = new StringBuilder();
            strSql.Append("update DepartmentTypeRel set ");
            strSql.Append("ParentID=@ParentID,");
            strSql.Append("ChildID=@ChildID");
            strSql.Append(" where ID=@ID");
            var parameters = new SqlParameter[] { new SqlParameter("@ParentID", SqlDbType.Int, 4),
                    new SqlParameter("@ChildID", SqlDbType.Int, 4),
                    new SqlParameter("@ID", SqlDbType.Int, 4) };
            parameters[0].Value = model.ParentID;
            parameters[1].Value = model.ChildID;
            parameters[2].Value = model.ID;

            var rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int ID)
        {
            var strSql = new StringBuilder();
            strSql.Append("delete from DepartmentTypeRel ");
            strSql.Append(" where ID=@ID");
            var parameters = new SqlParameter[] { new SqlParameter("@ID", SqlDbType.Int, 4)
            };
            parameters[0].Value = ID;

            var rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string IDlist )
        {
            var strSql = new StringBuilder();
            strSql.Append("delete from DepartmentTypeRel ");
            strSql.Append(" where ID in (" + IDlist + ")  ");
            var rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public DMZS.Model.DepartmentTypeRel GetModel(int ID)
        {
            var strSql = new StringBuilder();
            strSql.Append("select  top 1 ID,ParentID,ChildID from DepartmentTypeRel ");
            strSql.Append(" where ID=@ID");
            var parameters = new SqlParameter[] { new SqlParameter("@ID", SqlDbType.Int, 4)
            };
            parameters[0].Value = ID;
            var ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public DMZS.Model.DepartmentTypeRel DataRowToModel(DataRow row)
        {
            var model = new DMZS.Model.DepartmentTypeRel();
            if (row != null)
            {
                if (row["ID"] != null && row["ID"].ToString() != string.Empty)
                {
                    model.ID = int.Parse(row["ID"].ToString());
                }
                if (row["ParentID"] != null && row["ParentID"].ToString() != string.Empty)
                {
                    model.ParentID = int.Parse(row["ParentID"].ToString());
                }
                if (row["ChildID"] != null && row["ChildID"].ToString() != string.Empty)
                {
                    model.ChildID = int.Parse(row["ChildID"].ToString());
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            var strSql = new StringBuilder();
            strSql.Append("select ID,ParentID,ChildID ");
            strSql.Append(" FROM DepartmentTypeRel ");
            if (strWhere.Trim() != string.Empty)
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            var strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" ID,ParentID,ChildID ");
            strSql.Append(" FROM DepartmentTypeRel ");
            if (strWhere.Trim() != string.Empty)
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            var strSql = new StringBuilder();
            strSql.Append("select count(1) FROM DepartmentTypeRel ");
            if (strWhere.Trim() != string.Empty)
            {
                strSql.Append(" where " + strWhere);
            }
            var obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            var strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby );
            }
            else
            {
                strSql.Append("order by T.ID desc");
            }
            strSql.Append(")AS Row, T.*  from DepartmentTypeRel T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }
    }
}

